﻿// <copyright file="AppCache.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Core.Infrastructure
{
    using System;
    using System.Web;
    using System.Web.Caching;

    public class AppCache
    {
        private Cache Cache { get; set; }

        public AppCache()
        {
            this.Cache = HttpContext.Current.Cache;
        }

        public virtual void Add(string key,
                                   Object value,
                                   CacheDependency dependencies,
                                   DateTime absoluteExpiration,
                                   TimeSpan slidingExpiration,
                                   CacheItemPriority priority,
                                   CacheItemRemovedCallback onRemoveCallback)
        {
            this.Cache.Add(key,
                            value,
                            dependencies,
                            absoluteExpiration,
                            slidingExpiration,
                            priority,
                            onRemoveCallback);
        }
        
        public virtual void Add(string key, Object value)
        {
            if (this.Cache[key] == null)
            {
                this.Add(key, value, null, DateTime.Now.AddDays(7), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            else
            {
                this.Cache.Insert(key, value);
            }
        }

        public virtual object Get(string key)
        {
            return this.Cache.Get(key);
        }

        public virtual bool Exists(string key)
        {
            return this.Cache[key] != null;
        }

        public virtual void Remove(string key)
        {
            if (this.Exists(key))
                this.Cache.Remove(key);
        }

        public virtual void Remove(string[] kies)
        {
            foreach (var key in kies)
            {
                this.Remove(key);
            }
        }
    }
}
